package cn.gson.school.controller.jiuye;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gson.school.dao.jiuye.ResumeEntityDao;
import cn.gson.school.dao.jiuye.ResumeProjectEntityDao;
import cn.gson.school.entity.jiaoxue.StudentEntity;
import cn.gson.school.entity.jiuye.InterviewExperienceEntity;
import cn.gson.school.entity.jiuye.ResumeEntity;
import cn.gson.school.entity.jiuye.ResumeEvaluateEntity;
import cn.gson.school.entity.jiuye.ResumeProjectEntity;
import cn.gson.school.mappers.jiuye.ResumeEntityMapper;
import cn.gson.school.mappers.jiuye.ResumeEvaluateEntityMapper;
import cn.gson.school.mappers.jiuye.ResumeProjectEntityMapper;

	
/**
 * 简历管理
 * 
 * @author 华少
 *
 */
@Controller
public class QianTaiJianLIGuanli {
	@Autowired
	private ResumeEntityDao resumeEntityDao;

	@Autowired
	private ResumeEvaluateEntityMapper resumeEvaluateEntityMapper;
	
	@Autowired
	private ResumeProjectEntityDao resumeProjectEntityDao;
	
	@PostMapping({ "tianjiajianli" })
	@ResponseBody
	public void add(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException, ParseException {
		
		StudentEntity stu=	(StudentEntity) session.getAttribute("userinfo");

		//查询简历表中是否存在该用户的信息
		ResumeEntity res= resumeEntityDao.chaxunduixian(stu.getStuId());
		ResumeEntity resume=new ResumeEntity();
		if(null!=res){
			resume.setResumeId(res.getResumeId());
		}
		//添加学生的简历表
		String name=request.getParameter("resumeName");
		String xb=request.getParameter("resumeSex");
		String xs=request.getParameter("resumeSalary");
		String csrq=request.getParameter("resumeComedate");
		String lxdianhua=request.getParameter("resumeTelephone");
		String youjian=request.getParameter("resumeMailbox");
		String qq=request.getParameter("resumeQQ");
		String xuexiao=request.getParameter("resumeGraduation");
		String ziwopj=request.getParameter("resumeSelf");
		resume.setResumeName(name);
		resume.setResumeQQ(qq);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		resume.setResumeComedate(df.parse(csrq));
		resume.setResumeGraduation(xuexiao);
		resume.setResumeSalary(xs);
		resume.setResumeSex(Integer.valueOf(xb));
		resume.setResumeTelephone(lxdianhua);
		resume.setResumeMailbox(youjian);
		resume.setResumeSelf(ziwopj);
		resume.setStudentinfoId(stu);
		resume.setResumeTime(new Date());
		System.out.println("简历："+resume);
		resumeEntityDao.save(resume);
		//添加学生的项目简历
		List<String> xmname=new ArrayList<>();
		List<String> xmliangdian=new ArrayList<>();
		for(int i=1;i<=3;i++) {
			xmname.add(request.getParameter("resumeProjectName"+i).equals("")?" ":request.getParameter("resumeProjectName"+i));
		}
		for(int i=1;i<=3;i++) {
			xmliangdian.add(request.getParameter("resumeProjectHighlights"+i).equals("")?" ":request.getParameter("resumeProjectHighlights"+i));
		}
		ResumeEntity re= resumeEntityDao.chaxunduixian(stu.getStuId());
		List<ResumeProjectEntity> shu=resumeProjectEntityDao.chaxunxiangmu(re.getResumeId());
		System.out.println(shu);
		for(int i=0;i<xmname.size();i++) {
			ResumeProjectEntity project=new ResumeProjectEntity();
			if(!shu.isEmpty()){
				project.setResumeProjectId(shu.get(i).getResumeProjectId());
			}
			project.setResumeProjectName(xmname.get(i));
			project.setResumeProjectHighlights(xmliangdian.get(i));
			project.setResumeId(re);
			resumeProjectEntityDao.save(project);
		}
		  response.sendRedirect("chaxuijianliguanli?id="+stu.getStuId());
	}
	
	@GetMapping({"chaxuijianliguanli" })
	public String select(Model model, HttpServletRequest request) {
		String ids = request.getParameter("id");
		ResumeEntity re= resumeEntityDao.chaxunduixian(Long.valueOf(ids));
		if(re!=null){
			List<ResumeProjectEntity> project=resumeProjectEntityDao.chaxunxiangmu(re.getResumeId());
			List<ResumeEvaluateEntity> evaluate=resumeEvaluateEntityMapper.chaxunpingjia(re.getResumeId());
			model.addAttribute("resume",re);
			for(int i=0;i<=project.size()-1;i++) {
				model.addAttribute("highlights"+i,project.get(i).getResumeProjectHighlights());
				model.addAttribute("name"+i,project.get(i).getResumeProjectName());
			}
			model.addAttribute("project",project);
			model.addAttribute("evaluate",evaluate);
		}
		return "qt-jy-jianli";
	}
	
	
	
	
	
}
